﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Canyala.Lagoon.Text;

namespace Canyala.Lagoon.Models
{
    public static class Progress
    {
        private static Dictionary<Guid, Tracker> _trackItems = new Dictionary<Guid,Tracker>();

        internal class Tracker
        {
            public Guid Id { get; private set; }            
            public int TotalCount { get; private set; }
            public int Current { get; internal set; }
            public float Percent { get { return Current / TotalCount; } }

            internal Tracker(int totalCount)
            {
                Id = Guid.NewGuid();
                TotalCount = totalCount;
            }
        }

        public static Guid BeginTracking(int totalCount)
        {              
            var item = new Tracker(totalCount);
            _trackItems.Add(item.Id, item);
                
            return item.Id;
        }

        public static void EndTracking(Guid progressId)
            { _trackItems.Remove(progressId); }

        public static void Increase(Guid progressId)
        {    if (_trackItems[progressId].Current < _trackItems[progressId].TotalCount) 
                _trackItems[progressId].Current++;
        }

        public static void Set(Guid progressId, int value)
        { _trackItems[progressId].Current = value > _trackItems[progressId].TotalCount ? _trackItems[progressId].TotalCount : value; }

        public static void Reset(Guid progressId)
            { _trackItems[progressId].Current = 0; }

        public static int Get(Guid progressId)
            { return _trackItems[progressId].Current; }

        public static float GetPercent(Guid progressId)
            { return _trackItems[progressId].Percent; }

    }
}
